import {RouterObj} from '@/routers/interface/index'
// import type { MenuProps } from 'antd';
// type MenuItem = Required<MenuProps>["items"][number];

export const searchRoute = (path: string, routes:any[]) => {
	let result: RouterObj = {};
	for (let item of routes) {
		if (item.path === path) return item;
		if (item.children) {
			const res = searchRoute(path, item.children);
			if (Object.keys(res).length) result = res;
		}
	}
	return result;
};

export const findItemWithPath = (objArray: any[],valueToFind: string,path: any[]):any[] => {
   let parent:any[] = []
   for (let i=0;i<objArray.length;i++) {
        if(objArray[i]){
             // 当前路径（包括当前层级的对象）
             const currentPath:any[] = path.concat({...objArray[i]});
             // 检查当前项的属性值是否匹配
             if (objArray[i]?.key === valueToFind) {
               parent = [...currentPath]
               return currentPath; // 找到匹配值，返回当前路径
             }

             // 如果当前项包含子项，递归遍历子项
             if ('children' in objArray[i]) {
                   const result = findItemWithPath(objArray[i].children , valueToFind, currentPath);
                   if (result) {
                     parent = [...result]
                     return result; // 如果在子项中找到匹配值，返回结果
                   }
             }
        }
   }
   // 如果没有找到匹配值，返回path
   return parent
  };

  export const findParents = (objArray: any[],valueToFind: string,path:any[])=>{
     let allparents:any[] = path
     let findele = (objArray: any[],valueToFind: string) => {
       for(let i=0;i<objArray.length;i++){
             let item = objArray[i]
             if(item !== undefined && item !== null && item !== ''){
                 if(item.key === valueToFind){
                     allparents.push(item)
                     return [item]
                  }else{
                    if(item.children){
                       let result:any[] = findele(item.children,valueToFind)
                       if(result.length>0){
                          allparents.unshift(item)
                          return result;
                       }else{
                          allparents = []
                       }
                  }
             }
         }
       }
       return []
     }
     findele(objArray,valueToFind)
     return allparents
   };


   //扁平化后的数组
   export function flatArray(array:any[]):any[]{
     let newArr:any[] =  []
     function flatData(array:any[]):void {
       array.forEach(item => {
           if(item.children !== undefined && Array.isArray(item.children)){
              if(item.children.length===0){
                  newArr.push(item)
              }
              if(item.children.length>0){
                 flatData(item.children)
              }else{
                 item.children = ''
              }
           }else{
              newArr.push(item)
           }
       })
     }
     flatData(array)
     return newArr
   }


